#!/usr/bin/php

<?php

@error_reporting(E_ALL ^ E_NOTICE);
@set_time_limit(0);

require('./includes/dbconfig.php');
require('./includes/api/class.api.php');
require('./includes/api/class.transactions.php');

//Connect to DB
$link = mysql_connect($dbserver, $dbuser, $dbpass)
or die('Could not connect: ' . mysql_error());
mysql_select_db($dbase) or die('Could not select database');

// Retrieve API keys and fetch extra details from API
$sql    = 'SELECT * FROM `apikeys` WHERE `director` = 1';
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$api = new Api();

// Set API full logon details for corp info access
$api->setCredentials($row['userID'], $row['apiKey'], $row['charID']);
$corp = true;
$apitransxml  = $api->getWalletTransactions(null,$corp);
$apitrans     = Transaction::getTransaction($apitransxml);

if(is_array($apitrans)) {
  foreach($apitrans as $trans) {
    $sql = "INSERT IGNORE INTO `transactions` VALUES('".$trans['transactionDateTime']."',".$trans['transactionID'].",".$trans['quantity']."
  ,'".$trans['typeName']."',".$trans['typeID'].",".$trans['price'].",".$trans['clientID'].",'".$trans['clientName']."',".$trans['characterID']."
  ,'".$trans['characterName']."',".$trans['stationID'].",'".$trans['stationName']."','".$trans['transactionType']."','".$trans['transactionFor']."'
  );";
    mysql_query($sql);
  }
} else {
  echo 'Need Ful API key for '.$row['username'].'\'s character with charID: '.$row['charID'].'<br />';
}

$sql    = 'SELECT * FROM `apikeys`';
$result = mysql_query($sql);
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
  $corp = false;

  // Set API full logon details for persoanl info access
  $api->setCredentials($row['userID'], $row['apiKey'], $row['charID']);

  // Get XML and process using API class
  $apitransxml  = $api->getWalletTransactions(null,$corp);
  $apitrans     = array();
  $apitrans     = Transaction::getTransaction($apitransxml);
  if(is_array($apitrans)) {
    foreach($apitrans as $trans) {
      $sql = "INSERT IGNORE INTO `transactions` VALUES('".$trans['transactionDateTime']."',".$trans['transactionID'].",".$trans['quantity']."
  ,'".$trans['typeName']."',".$trans['typeID'].",".$trans['price'].",".$trans['clientID'].",'".$trans['clientName']."',".$row['charID']."
  ,NULL,".$trans['stationID'].",'".$trans['stationName']."','".$trans['transactionType']."','".$trans['transactionFor']."'
  );";
      mysql_query($sql);
    }
  } else {
    echo 'Need Full API key for '.$row['username'].'\'s character with charID: '.$row['charID'].'<br />';
  }
}
?>